/**
 * simple package of Ajax methods
 * default send by json
 */
var ajax = function(){
	var contextpath = null;
	function exec(configs){
		if(typeof(configs) == 'undefined'){
			throw new Error("configs are in need !");
		}
		if(typeof(configs.url) == 'undefined'){
			throw new Error("url can't be null");
		}
		if(typeof(configs.success) == 'undefined'){
			throw new Error("success method is in need ");
		}
		var xhr = null;
		if(window.XMLHttpRequest){
			xhr = new XMLHttpRequest();
		}else{
			xhr = new ActiveXObject('Microsoft.XMLHTTP');
		}
		xhr.onreadystatechange = resultHandler(xhr,configs);
		if(typeof(configs.method) == 'undefined'){
			configs.method = "GET";
		}
		if(typeof(configs.async) == 'undefined'){
			configs.async = true;
		}
		if(typeof(configs.path) == 'undefined'){
			configs.url = contextPath(configs)+configs.url;
		}
		resolveData(configs);
		var contentType = null;
		var sendDatas = null;
		if(configs.dataType.toUpperCase() == "JSON"){
			contentType = "application/json;charset=UTF-8";
		}else if(configs.dataType.toUpperCase() == "FILE"){
			contentType = null;
		}else{
			contentType = "text/html;charset=UTF-8";
		}
		if(configs.method.toUpperCase() == "GET"){
			if(configs.url.indexOf("?") < 0){
				configs.url = configs.url +"?"+configs.datas;
			}else{
				configs.url = configs.url +"&"+configs.data;
			}
		}else{
			sendDatas = configs.datas;
		}
		xhr.open(configs.method,configs.url,configs.async);
		if(null != contentType){
			xhr.setRequestHeader("Content-Type",contentType);
		}
		xhr.send(sendDatas);
	}
	function resolveData(configs){
		var datas = configs.datas;
		var dataType = configs.dataType;
		if(typeof(datas) == 'undefined'){
			datas = {};
		}
		if(typeof(dataType) == 'undefined'){
			dataType = "JSON";
		}
		if(dataType.toUpperCase() =="JSON" ){
			if(datas instanceof Object){
				datas = JSON.stringify(datas);
			}
//			datas = encodeStr(datas);
		}else if(dataType.toUpperCase() == "FILE"){
		}else{
			datas = encodeParam(datas);
			datas = params(datas);
		}
		configs.datas = datas;
		configs.dataType = dataType;
	}
	function resultHandler(xhr,configs){
		var handle = function(){
			if(xhr.readyState == 4){
				if(xhr.status >= 200 &&xhr.status <300){
					var result = xhr.responseText;
					if(configs.dataType.toUpperCase() == "JSON" ||
							configs.dataType.toUpperCase() == "FILE"){
						result = JSON.parse(result);
					}
					configs.success(result,xhr.responseXML);
				}else{
					if(typeof(configs.error) != 'undefined'){
						configs.error(xhr.responseText,xhr.responseXML);
					}else{
						throw new Error("ajax occured error !");
					}
				}
			}
		};
		return handle;
	};
	function encodeParam(datas){
		var result = {};
		if(datas instanceof Object){
			for(var index in datas){
				var key = encodeURIComponent(index);
				var value = encodeURIComponent(datas[index]);
				result[key] = value;
			}
		}else{
			result['string'] = encodeStr(datas);
		}
		return result;
	};
	function encodeStr(datas){
		return encodeURIComponent(datas);
	}
	function params(datas){
		var params = [];
		for(var index in datas){
			params.push(index+"="+datas[index]);
		}
		return params.join("&");
	};
	function contextPath(configs){
		if(typeof(configs.contextpath) == 'undefined'){
			if(null == contextpath){
				var pathname = window.location.pathname;
				var reg = new RegExp("^/\\w*");
				contextpath = reg.exec(pathname)+"/";
			}
		}else{
			contextpath = configs.contextpath+"/";
		}
		return contextpath;
	}
	return exec;
}();